package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.AdChannelService;
import com.heima.model.admin.dtos.ChannelDto;
import com.heima.model.admin.pojos.AdChannel;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

@Service
@Transactional
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements AdChannelService {

    /**
     * 根据名称分页查询频道列表并排序
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findByNameAndPage(ChannelDto dto) {
        // 参数判断
        if (dto == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        dto.checkParam();

        // 分页查询
        IPage<AdChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        // 非空 加模糊查询
        if (StringUtils.isNotBlank(dto.getName())) {
            wrapper.like(AdChannel::getName, dto.getName());
        }
        // 排序 根据id升序
        wrapper.orderByAsc(AdChannel::getOrd);
        IPage<AdChannel> iPage = this.page(page, wrapper);
        // 返回结果
        return new PageResponseResult(dto.getPage(), dto.getSize(), (int) iPage.getTotal(), iPage.getRecords());
    }

    /**
     * 新增
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult insert(AdChannel adChannel) {
        // 参数校验
        if (adChannel == null || adChannel.getIsDefault() == null || StringUtils.isBlank(adChannel.getName()) || adChannel.getOrd() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断排序是否存在
        if (this.getOne(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getOrd, adChannel.getOrd())) != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ORD_EXIST);
        }
        // 判断频道名称是否存在
        if (this.getOne(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getName, adChannel.getName())) != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 数据库操作
        adChannel.setCreatedTime(new Date());
        // 判断是否是默认频道
        if (adChannel.getIsDefault()) {
            LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
            AdChannel one = this.getOne(wrapper.eq(AdChannel::getIsDefault, 1));
            // 判断是否有默认频道,有就改为非默认
            if (one != null) {
                one.setIsDefault(false);
                this.updateById(one);
            }
        }
        this.save(adChannel);
        // 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改&状态
     *
     * @param adChannel
     * @return
     */
    @Override
    public ResponseResult update(AdChannel adChannel) {
        // 参数校验
        if (adChannel == null || adChannel.getIsDefault() == null || StringUtils.isBlank(adChannel.getName()) || adChannel.getOrd() == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        // 判断频道名称是否存在
        if (this.getOne(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getName, adChannel.getName())) != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_EXIST);
        }
        // 判断排序是否存在
        if (this.getOne(new LambdaQueryWrapper<AdChannel>().eq(AdChannel::getOrd, adChannel.getOrd())) != null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ORD_EXIST);
        }
        // 数据库操作
        adChannel.setCreatedTime(new Date());
        // 判断是否是默认频道
        if (adChannel.getIsDefault()) {
            LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
            AdChannel one = this.getOne(wrapper.eq(AdChannel::getIsDefault, 1));
            // 判断是否有默认频道,有就改为非默认
            if (one != null) {
                one.setIsDefault(false);
                this.updateById(one);
            }
        }
        this.updateById(adChannel);
        // 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleteById(Integer id) {
        // 参数校验
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_REQUIRE);
        }
        //判断id是否不存在，频道是否非默认，频道状态是否禁用
        AdChannel adChannel = this.getById(id);
        if (adChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.DATA_NOT_EXIST);
        }
        if (adChannel.getIsDefault()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ISDEFAULT_INVALID);
        }
        if (adChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.ADMIN_CHANNEL_ISSTATUS_ERROR);
        }
        // 数据库操作
        this.removeById(id);
        // 返回结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }
}
